import { createRouter, createWebHistory } from 'vue-router';
import LoginView from '@/views/LoginView/LoginView.vue';
import RegisterView from "@/views/loginView/RegisterView.vue";
import TicTacToe from "@/views/gameView/ticTacToe/ticTacToe.vue";
import GameLobby from "@/views/homeView/GameLobby.vue";
import GameRoomView from "@/views/homeView/GameRoomView.vue";
import RoomPlayerListView from "@/views/homeView/RoomPlayerListView.vue";
import SuperTicTacToe from "@/views/gameView/superTicTacToe/UltimateSuperTicTacToe.vue";
import {useAuthStore} from "@/stores/auth.js";



const routes = [
    {
        path: '/login',
        name: 'Login',
        component: LoginView,
    },
    {
        path: '/',
        redirect: '/login', // 默认跳转到登录页
    },
    {
        path: '/register',
        name: 'Register',
        component: RegisterView,
    },
    {
        path: '/home',
        name: 'GameLobby',
        component: GameLobby,
    },
    {
        path: '/gameRoom/:gameId',
        name: 'GameRoom',
        component: GameRoomView,
    },
    {
       path: '/Tictactoe',
       name: 'Tic-tac-toe',
       component: TicTacToe,
    },
    {
       path: '/SuperTicTacToe',
       name: 'SuperTicTacToe',
       component: SuperTicTacToe,
    },
    {
        path: '/gameRoom/:gameId/:roomId',
        name: 'RoomPlayerList',
        component: RoomPlayerListView,
    },

];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const authStore = useAuthStore();
    const publicPages = ['/login', '/register','/static'];
    const authRequired = !publicPages.includes(to.path);
    const token = localStorage.getItem('token');

    if (authRequired && !token) {
        return next('/login');
    }

    next();
});

export default router;
